//request.js
import axios from "axios"
// 导入本地缓存的token
// import store from '@/store'
const instance = axios.create({
  //接口域名
  baseURL: 'https://api-hmugo-web.itheima.net/',
  timeout: 5000, //表示请求超过时间

})

//请求拦截
instance.interceptors.request.use(
  function (config) {
    // let token = store.getters.getToken
    //这里添加token验证
    // if (token) {
    //   console.log(token)
    //   把token复制给请求头
    //   config.headers.authorization = token
    // }
    return config
  },
  function (error) {
    // 请求失败需要操作的，比如token失效返回登录页
    return Promise.reject(error)
  }
)

//响应拦截
instance.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)

//封装get请求 axios.get(url,{params:params})
export const get = (url, params) => instance.get(url, { params })
//封装post请求 axios.post(url,{name:'阿边',age:23})
export const post = (url, data) => instance.post(url, data)
//封装删除接口
export const del = (url, params) => instance.delete(url, { params })
//封装修改接口
export const put = (url, data) => instance.put(url, data)
//export default instance;
